import 'package:wk_robot2/app/blt/protobuf/BleBindOrSwitchWifi.pb.dart';
import 'package:wk_robot2/app/blt/protobuf/BluetoothHandShake.pb.dart';
import 'package:wk_robot2/app/blt/protobuf/BluetoothHandShake.pbenum.dart';

import '../const/AppConst.dart';
import '../event/DataCallBack.dart';
import 'RobotCommands.dart';

class RobotCommandsHelper {


  //蓝牙连接成功后,第一个蓝牙指令.握手.
  shakeHandWithRobot(DataCallBack dataCallBack,{String clientLan = 'zh',clientVersionCode = 1266,clientVersionName = 'v2.2.11',deviceCountry = 'CN',
    DeviceType deviceType = DeviceType.ANDROID_MOBILE}) {

    //蓝牙通信协议请求体.
    BluetoothHandShakeRequest requestBody = BluetoothHandShakeRequest()
      ..clientLan = clientLan
      ..clientVersionCode = clientVersionCode
      ..clientVersionName = clientVersionName
      ..deviceCountry = deviceCountry
      ..deviceType =  deviceType;
    // 请求协议头.
    HardwareCommands.instance.sendMsgToRobot({
      AppConst.PROTOCOL_HEADER_COMMADN : HardwareCommands.BL_NEW_HAND_SHAKE_REQUEST,
      AppConst.PROTOCOL_RPOTO_VERSION : 1,
      AppConst.PROTOCOL_REQUEST_SEQUENCE : HardwareCommands.instance.getRequestSequence(),
      AppConst.PROTOCOL_RESPONSE_SEQUENCE: HardwareCommands.instance.getResponseSequence()
    }, requestBody.writeToBuffer(),dataCallBack);

  }


  sendHandShake(DataCallBack dataCallBack)  {
    BindOrSwitchWifiRequest requestBody = BindOrSwitchWifiRequest()
      ..data = HardwareCommands.instance.pactkCommandToRobot(HardwareCommands.CONNECT_SUCCESS)
      ..versionCode = HardwareCommands.ROBOT_COMMAND_VERSION;



    HardwareCommands.instance.sendMsgToRobot({
      AppConst.PROTOCOL_HEADER_COMMADN : HardwareCommands.BL_BIND_OR_SWITCH_WIFI_REQUEST,
      AppConst.PROTOCOL_RPOTO_VERSION : 1,
      AppConst.PROTOCOL_REQUEST_SEQUENCE : HardwareCommands.instance.getRequestSequence(),
      AppConst.PROTOCOL_RESPONSE_SEQUENCE: HardwareCommands.instance.getResponseSequence()
    }, requestBody.writeToBuffer(),dataCallBack);
  }




  //检查robot网络状态.
  sendWifiIsOkMsgToRobot(DataCallBack dataCallBack) {

    BindOrSwitchWifiRequest requestBody = BindOrSwitchWifiRequest()
                                          ..data = HardwareCommands.instance.pactkCommandToRobot(HardwareCommands.ROBOT_WIFI_IS_OK_TRANS)
                                          ..versionCode = HardwareCommands.ROBOT_COMMAND_VERSION;



    HardwareCommands.instance.sendMsgToRobot({
      AppConst.PROTOCOL_HEADER_COMMADN : HardwareCommands.BL_BIND_OR_SWITCH_WIFI_REQUEST,
      AppConst.PROTOCOL_RPOTO_VERSION : 1,
      AppConst.PROTOCOL_REQUEST_SEQUENCE : HardwareCommands.instance.getRequestSequence(),
      AppConst.PROTOCOL_RESPONSE_SEQUENCE: HardwareCommands.instance.getResponseSequence()
    }, requestBody.writeToBuffer(),dataCallBack);


  }


  //获取wifi列表.
  getWifiList(DataCallBack dataCallBack) {

    BindOrSwitchWifiRequest requestBody = BindOrSwitchWifiRequest()
      ..data = HardwareCommands.instance.pactkCommandToRobot(HardwareCommands.WIFI_LIST_TRANS)
      ..versionCode = HardwareCommands.ROBOT_COMMAND_VERSION;



    HardwareCommands().sendMsgToRobot({
      AppConst.PROTOCOL_HEADER_COMMADN : HardwareCommands.BL_BIND_OR_SWITCH_WIFI_REQUEST,
      AppConst.PROTOCOL_RPOTO_VERSION : 1,
      AppConst.PROTOCOL_REQUEST_SEQUENCE : HardwareCommands.instance.getRequestSequence(),
      AppConst.PROTOCOL_RESPONSE_SEQUENCE: HardwareCommands.instance.getResponseSequence()
    }, requestBody.writeToBuffer(),dataCallBack);


  }


  //设置wifi密码.
  configWifiList(String type, String name, String passwd, DataCallBack dataCallBack) {
    BindOrSwitchWifiRequest requestBody = BindOrSwitchWifiRequest()
      ..data = HardwareCommands.instance.setWifiPasswdInfo(type,name,passwd)
      ..versionCode = HardwareCommands.ROBOT_COMMAND_VERSION;



    HardwareCommands().sendMsgToRobot({
      AppConst.PROTOCOL_HEADER_COMMADN : HardwareCommands.BL_BIND_OR_SWITCH_WIFI_REQUEST,
      AppConst.PROTOCOL_RPOTO_VERSION : 1,
      AppConst.PROTOCOL_REQUEST_SEQUENCE : HardwareCommands.instance.getRequestSequence(),
      AppConst.PROTOCOL_RESPONSE_SEQUENCE: HardwareCommands.instance.getResponseSequence()
    }, requestBody.writeToBuffer(),dataCallBack);


  }

  //发送消息到wifi.
  sendInfo2WifiList(String type, String name, String passwd, DataCallBack dataCallBack) {
    BindOrSwitchWifiRequest requestBody = BindOrSwitchWifiRequest()
      ..data = HardwareCommands.instance.setWifiPasswdInfo(type,name,passwd,co:HardwareCommands.WIFI_INFO_TRANS)
      ..versionCode = HardwareCommands.ROBOT_COMMAND_VERSION;



    HardwareCommands().sendMsgToRobot({
      AppConst.PROTOCOL_HEADER_COMMADN : HardwareCommands.BL_BIND_OR_SWITCH_WIFI_REQUEST,
      AppConst.PROTOCOL_RPOTO_VERSION : 1,
      AppConst.PROTOCOL_REQUEST_SEQUENCE : HardwareCommands.instance.getRequestSequence(),
      AppConst.PROTOCOL_RESPONSE_SEQUENCE: HardwareCommands.instance.getResponseSequence()
    }, requestBody.writeToBuffer(),dataCallBack);


  }



  // 发送agent到Robot.
  sendBindResultAndAgent2Robot(bool bind,String agentToken,String refreshAgentToken, DataCallBack dataCallBack) {
    BindOrSwitchWifiRequest requestBody = BindOrSwitchWifiRequest()
      ..data = HardwareCommands.instance.pacKBindingResultCommandToRobot(bind,agentToken,refreshAgentToken)
      ..versionCode = HardwareCommands.ROBOT_COMMAND_VERSION;



    HardwareCommands().sendMsgToRobot({
      AppConst.PROTOCOL_HEADER_COMMADN : HardwareCommands.BL_BIND_OR_SWITCH_WIFI_REQUEST,
      AppConst.PROTOCOL_RPOTO_VERSION : 1,
      AppConst.PROTOCOL_REQUEST_SEQUENCE : HardwareCommands.instance.getRequestSequence(),
      AppConst.PROTOCOL_RESPONSE_SEQUENCE: HardwareCommands.instance.getResponseSequence()
    }, requestBody.writeToBuffer(),dataCallBack);


  }




}